package com.desksoft.ecommerce.handler.controller.logistics;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import com.desksoft.utility.pagination.Page;
import com.google.common.collect.ImmutableMap;

/**
 * 在线POS controller父类
 * @author Administrator
 *
 */
public class LogisticsPosOnlineControllerBase extends LogisticsControllerBase {

	/**
	 * 组装成jqGrid可识别的数据
	 * 
	 * @param <E>
	 * @param records
	 * @return
	 */
	public <E> Map<String, Object> render4jqgrid(List<E> records) {
		Page<E> page = new Page<E>();
		page.setRecordsList(records);
		return this.render4jqgrid(page);
	}
	
	/**
	 * 组装成jqGrid可识别的数据
	 * 
	 * @param <V>
	 * @param page
	 * @return
	 */
	public <V> Map<String, Object> render4jqgrid(Page<V> page) {
		return this.render4jqgrid(page, new HashMap<String, Object>());
	}
	
	/**
	 * 组装成jqGrid可识别的数据
	 * 
	 * @param <V>
	 * @param page
	 * @param userdata
	 * @return
	 */
	public <V> Map<String, Object> render4jqgrid(Page<V> page, Map<String, Object> userdata) {
		List<V> l = page.getRecordsList();
		
		if (CollectionUtils.isNotEmpty(l)) {
			userdata.put("clazz", l.get(0).getClass().getName());
		}
		
		return ImmutableMap.of(
			"page", page.getPageNo(),
			"total", page.getPageNumber(),
			"records", page.getTotalCount(),
			"rows", l,
			"userdata", userdata
		);
	}
	
	/**
	 * 组装成query_table可识别的数据
	 * 		page:			当前页码
	 * 		total:			总页数
	 * 		pageSize:		每页记录数
	 * 		records:			总记录数
	 * 		rows:			查询结果数组
	 */		
	public <V> Map<String, Object> render4Querytable(Page<V> page){
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("page", page.getPageNo());
		map.put("total", page.getPageNumber());
		map.put("pageSize", page.getPageSize());
		map.put("records", page.getTotalCount());
		map.put("rows", page.getRecordsList());
		return map;
	}
	
}
